from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, PasswordField
from wtforms.validators import DataRequired, length, EqualTo, ValidationError
from market.models import User


class RegisterForm(FlaskForm):

    def validate_username(self, username_to_check):
        user = User.query.filter_by(username=username_to_check.data).first()
        if user:
            raise ValidationError('该用户已经存在')



    username = StringField(label='Username', validators=[length(min=2, max=30), DataRequired()])
    password1 = PasswordField(label='Password', validators=[length(min=6), DataRequired()])
    password2 = PasswordField(label='Confirm Password', validators=[EqualTo('password1'), DataRequired()])
    submit = SubmitField(label='Submit')



class LoginForm(FlaskForm):
    username = StringField(label='Username', validators=[length(min=2, max=30), DataRequired()])
    password = PasswordField(label='Password', validators=[length(min=6), DataRequired()])
    submit = SubmitField(label='Submit')


